package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmHttpRequestDefinition;
import com.harmontronics.hanma.paas.paas.dto.input.HmHttpRequestDefinitionCreateInput;
import com.harmontronics.hanma.paas.paas.dto.input.HmHttpRequestDefinitionUpdateInput;
import com.harmontronics.hanma.paas.paas.dto.input.RequestExecutionInput;
import com.harmontronics.hanma.paas.paas.dto.output.HmHttpRequestDefinitionResult;
import com.harmontronics.hanma.paas.paas.service.HmHttpRequestDefinitionAppService;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;


/**
 * 请求定义
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-05-18 10:20:57
 */
@RestController
@RequestMapping("/api/hm-http-request-definition")
@Api(tags = {"请求定义"})
public class HmHttpRequestDefinitionController {
    @Autowired
    private HmHttpRequestDefinitionAppService hmHttpRequestDefinitionappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmHttpRequestDefinitionResult>> list(@ApiParam(value = "模型ID") @RequestParam Long modelId) {
        List<HmHttpRequestDefinition> hmHttpRequestDefinitionList = hmHttpRequestDefinitionappService.getList(modelId);

        return ResponseEntity.ok(Convert.toList(HmHttpRequestDefinitionResult.class, hmHttpRequestDefinitionList));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmHttpRequestDefinitionResult>> list(
            @ApiParam(value = "模型ID") @RequestParam Long modelId,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                                        @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmHttpRequestDefinition> hmHttpRequestDefinitionPageBase = hmHttpRequestDefinitionappService.getList(modelId, pageNo, pageSize);
        PageBase<HmHttpRequestDefinitionResult> resultPageBase = PageBase.copy(hmHttpRequestDefinitionPageBase, HmHttpRequestDefinitionResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmHttpRequestDefinitionResult> info(@RequestParam("id") Long id) {
        HmHttpRequestDefinition hmHttpRequestDefinition = hmHttpRequestDefinitionappService.findById(id);
        HmHttpRequestDefinitionResult result = BeanUtil.toBean(hmHttpRequestDefinition, HmHttpRequestDefinitionResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmHttpRequestDefinitionCreateInput hmHttpRequestDefinitionInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmHttpRequestDefinition hmHttpRequestDefinition = BeanUtil.toBean(hmHttpRequestDefinitionInput, HmHttpRequestDefinition.class);
        hmHttpRequestDefinitionappService.save(hmHttpRequestDefinition);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmHttpRequestDefinitionUpdateInput hmHttpRequestDefinitionInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmHttpRequestDefinition hmHttpRequestDefinition = BeanUtil.toBean(hmHttpRequestDefinitionInput, HmHttpRequestDefinition.class);
        hmHttpRequestDefinitionappService.update(hmHttpRequestDefinition);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmHttpRequestDefinitionappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }

    @PostMapping("execute")
    @ApiOperation("执行请求")
    public ResponseEntity<?> execute(@RequestBody RequestExecutionInput input) throws SQLException, IOException {
        return hmHttpRequestDefinitionappService.execute(input.getModelKey(),input.getPageId(),input.getKey(),input.getValues(),input.getPageNo(),input.getPageSize(),input.getOrderRules());

    }


}
